import time

from selenium.webdriver.common.by import By

from common.Utils import TestDriver


# 测试问卷考试系统个人设置页面
class SettingTest:
    url = ""
    driver = ""

    def __init__(self):
        self.url = "http://8.155.1.153:8081/system/setting"
        self.driver = TestDriver.driver
        self.driver.get(self.url)

    # 进行页面元素的查找检查
    def SettingCheckElement(self):
        # 检查页面标题
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.title___3cGwa")
        # 检查更换头像按钮
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.right___3-EXe > span > div > span > div > button")
        # 检查姓名标签
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.left___GavW0 > form > div:nth-child(2) > div > div.ant-col.ant-form-item-label > label")
        # 检查姓名输入框
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.left___GavW0 > form > div:nth-child(2) > div > div.ant-col.ant-form-item-control > div > div > span")
        # 检查手机号标签
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.left___GavW0 > form > div:nth-child(3) > div > div.ant-col.ant-form-item-label > label")
        # 检查手机号输入框
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.left___GavW0 > form > div:nth-child(3) > div > div.ant-col.ant-form-item-control > div > div > span")
        # 检查邮箱标签
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.left___GavW0 > form > div:nth-child(4) > div > div.ant-col.ant-form-item-label > label")
        # 检查邮箱输入框
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.left___GavW0 > form > div:nth-child(4) > div > div.ant-col.ant-form-item-control > div > div > span")
        # 检查个人简介标签
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.left___GavW0 > form > div:nth-child(5) > div > div.ant-col.ant-form-item-label > label")
        # 检查个人简介输入框
        self.driver.find_element(By.CSS_SELECTOR, "#profile")
        # 检查提交按钮
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.left___GavW0 > form > div.ant-space.ant-space-horizontal.ant-space-align-center > div:nth-child(2) > button")

        # 添加屏幕截图
        TestDriver.getScreenshot()

        # 更新信息成功的测试用例
        def SettingSuccessTest(self):
            # 清空输入框
            TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#name"))
            TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#phone"))
            TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#email"))
            TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#profile"))
            # 添加屏幕截图
            TestDriver.getScreenshot()

            # 输入姓名
            self.driver.find_element(By.CSS_SELECTOR, "#name").send_keys("test")
            self.driver.find_element(By.CSS_SELECTOR, "#phone").send_keys("1")
            self.driver.find_element(By.CSS_SELECTOR, "#email").send_keys("1@qq.com")
            self.driver.find_element(By.CSS_SELECTOR, "#profile").send_keys("功能测试")
            # 点击提交按钮
            self.driver.find_element(By.CSS_SELECTOR,
                                     "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.left___GavW0 > form > div.ant-space.ant-space-horizontal.ant-space-align-center > div:nth-child(2) > button").click()

            TestDriver.getScreenshot()

            # 验证是否更新成功

            # 通过页面右上角的用户名来验证
            actual = self.driver.find_element(By.CSS_SELECTOR,
                                              "#sk-layout > div > div > section > div.ant-pro-layout-container > header.ant-layout-header.ant-pro-layout-header.ant-pro-layout-header-fixed-header.ant-pro-layout-header-mix.ant-pro-layout-header-fixed-header-action.ant-pro-layout-header-header > div > div.ant-pro-global-header-right-content > div > div > div > span > div > span:nth-child(2)").text
            print(f"姓名: {actual}")

            TestDriver.getScreenshot()
            # 断言检测一下是否符合预期
            assert actual == "test"

    # 更新信息失败的测试用例
    def SettingFailTest(self):
        # 测试用例一：不输入姓名，其他信息正常输入
        # 清空输入框
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#name"))
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#phone"))
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#email"))
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#profile"))

        TestDriver.getScreenshot()

        # 输入信息
        self.driver.find_element(By.CSS_SELECTOR, "#phone").send_keys("test1")
        self.driver.find_element(By.CSS_SELECTOR, "#email").send_keys("1@qq.com")
        self.driver.find_element(By.CSS_SELECTOR, "#profile").send_keys("功能测试")
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.left___GavW0 > form > div.ant-space.ant-space-horizontal.ant-space-align-center > div:nth-child(2) > button").click()
        time.sleep(1)
        TestDriver.getScreenshot()
        # 验证是否更新失败
        actual = self.driver.find_element(By.CSS_SELECTOR, "#name_help > div").text
        print(f"提示信息: {actual}")
        # 添加屏幕截图
        TestDriver.getScreenshot()
        # 断言检测一下是否符合预期
        assert actual == "请输入您的昵称!"

        ##########################################################################################################################

        # 测试用例二：不输入手机号，其他信息正常输入

        # 清空输入框
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#name"))
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#phone"))
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#email"))
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#profile"))

        TestDriver.getScreenshot()

        # 输入信息
        self.driver.find_element(By.CSS_SELECTOR, "#name").send_keys("test")
        self.driver.find_element(By.CSS_SELECTOR, "#email").send_keys("1@qq.com")
        self.driver.find_element(By.CSS_SELECTOR, "#profile").send_keys("功能测试")
        # 点击提交按钮
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.left___GavW0 > form > div.ant-space.ant-space-horizontal.ant-space-align-center > div:nth-child(2) > button").click()
        time.sleep(1)
        TestDriver.getScreenshot()
        # 验证是否更新失败
        actual = self.driver.find_element(By.CSS_SELECTOR, "#phone_help > div").text
        print(f"提示信息: {actual}")
        # 添加屏幕截图
        TestDriver.getScreenshot()
        # 断言检测一下是否符合预期
        assert actual == "请输入您的联系电话!"

        ##########################################################################################################################

        # 测试用例三：不输入邮箱，其他信息正常输入

        # 清空输入框
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#name"))
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#phone"))
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#email"))
        TestDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "#profile"))

        TestDriver.getScreenshot()

        # 输入信息
        self.driver.find_element(By.CSS_SELECTOR, "#name").send_keys("test")
        self.driver.find_element(By.CSS_SELECTOR, "#phone").send_keys("1")
        self.driver.find_element(By.CSS_SELECTOR, "#profile").send_keys("这是更新个人基本信息功能测试")
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.left___GavW0 > form > div.ant-space.ant-space-horizontal.ant-space-align-center > div:nth-child(2) > button").click()
        time.sleep(1)

        actual = self.driver.find_element(By.CSS_SELECTOR, "#email_help > div").text
        print(f"提示信息: {actual}")

        TestDriver.getScreenshot()
        # 断言检测一下是否符合预期
        assert actual == "请输入您的邮箱!"
